package com.gb.handler;

import com.gb.models.main.utils.ResultMsg;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;

/**
 * 全局异常处理
 *
 * @author lgoodbook@163.com
 * @version 1.0.0
 * @Date 2017-08-11 13:42
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = { ConstraintViolationException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultMsg constraintViolationException(ConstraintViolationException ex) {
        return new ResultMsg(ex);
    }

    @ExceptionHandler(value = { IllegalArgumentException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultMsg IllegalArgumentException(IllegalArgumentException ex) {
        return new ResultMsg(ex);
    }

    @ExceptionHandler(value = { NoHandlerFoundException.class })
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResultMsg noHandlerFoundException(Exception ex) {
        return new ResultMsg(ex);
    }

    @ExceptionHandler(value = { Exception.class })
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultMsg unknownException(Exception ex) {
        return new ResultMsg(ex);
    }
}
